﻿package GameUtils
{	
	import com.junkbyte.console.Cc;

	import flash.media.Sound;
	import flash.net.URLRequest;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.system.Security;	

	public class TrackData extends EventDispatcher
	{
		public var File:String;
		public var Name:String;
		public var Data:Vector.<MarkData>;
		public var Bots:Vector.<String>;
		public var Height:Number;
		public var ID:int;
		public var Track:Sound;
		
		private var FLoaded:Boolean;
		private var FLoading:Boolean;
		
		public function TrackData():void 
		{
			File = new String();
			Name = new String();
			Data = new Vector.<MarkData>;
			Bots = new Vector.<String>;
			Height = new Number();
			Track = new Sound();
			FLoaded = false;
			FLoading = false;
		}
		
		public function Load():void
		{
			if((!FLoading) && (!FLoaded))
			{
				UpdateSecurity(File);
				Track.addEventListener(Event.COMPLETE, OnSoundLoaded);
				Track.load(new URLRequest(File));
								
				FLoading = true;
			}
		}
		
		private function UpdateSecurity(APath:String):void
		{			
			Cc.info('Loading MP3 from ' + APath);
			
			var vHost:String = APath.substr(7, APath.length).split('.')[0];
			Security.loadPolicyFile("http://" + vHost + ".vkontakte.ru/crossdomain.xml");
			Security.loadPolicyFile("http://" + vHost + ".vk.com/crossdomain.xml");
		}
		
		private function OnSoundLoaded(e:Event):void
		{			
			FLoading = false;
			FLoaded = true;		
			
			dispatchEvent(new Event(Event.COMPLETE));
		}
		
		public function get IsLoaded():Boolean
		{
			return FLoaded;
		}
		
		public function get IsLoading():Boolean
		{
			return FLoading;
		}
	}	
}
